package org.clockworkmages.games.anno1186.gui;

import java.awt.Component;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;

import org.clockworkmages.games.anno1186.GameBeansContext;
import org.clockworkmages.games.anno1186.GameDataService;
import org.clockworkmages.games.anno1186.model.option.Option;
import org.clockworkmages.games.anno1186.text.TextUtil;

public class OptionListCellRenderer implements ListCellRenderer<Option> {

	// private final ListCellRenderer<Option> defaultRenderer;
	//
	// public OptionListCellRenderer(ListCellRenderer<Option> cellRenderer) {
	// this.defaultRenderer = cellRenderer;
	// }

	@Override
	public Component getListCellRendererComponent(JList<? extends Option> list,
			Option option, int index, boolean isSelected, boolean cellHasFocus) {
		// JLabel result =
		// (JLabel)defaultRenderer.getListCellRendererComponent(list,
		// value, index, isSelected, cellHasFocus);
		GameDataService gameDataService = GameBeansContext
				.getBean(GameDataService.class);
		Map<String, ImageIcon> imageIcons = gameDataService.getImageIcons();
		String text = (index + 1) + ". " + TextUtil.parse(option.getLabel());
		ImageIcon imageIcon = null;
		if (option.getIcon() != null) {
			imageIcon = imageIcons.get(option.getIcon());
			if (imageIcon == null) {
				GameUiUtil.logError("Invalid icon reference - "
						+ option.getIcon());
			}
		}
		if (imageIcon == null) {
			imageIcon = imageIcons.get("none");
		}
		JLabel result = new JLabel(text, imageIcon, SwingConstants.LEFT);
		return result;
	}
}
